/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/3
 */
import type { IDefaultSetting } from '@/utils/setting'
import { ref } from 'vue'
import { defineStore } from 'pinia'
import { defaultSetting } from '@/utils'

export const useSetting = defineStore(
    'setting',
    () => {
        const setting = ref<IDefaultSetting>()
        const init = () => {
            if (setting.value === undefined) {
                setting.value = {} as any
            }
            const config = (window as any)['pt_config'] ?? {}
            setting.value = Object.assign({}, defaultSetting, setting.value, config)
        }

        const getSetting = (key: string) => {
            if (setting.value === undefined) {
                return defaultSetting[key] ?? undefined
            }
            return setting.value[key] ?? defaultSetting[key] ?? undefined
        }

        const setSetting = (key: string, val: any) => {
            setting.value![key] = val
        }

        return {
            getSetting,
            setSetting,
            init,
            setting,
        }
    },
    {
        persist: {
            enabled: true,
            strategies: [
                {
                    key: 'style', //自定义 Key值
                    storage: localStorage, // 选择存储方式
                },
            ],
        },
    },
)
